package com.bincky.common;

import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.sql.Driver;

/**
 * 描述:
 *
 * @author Bincky Liang
 * @date 2021-02-08 14:27:10
 */

public class BasePage {

    //初始化日志对象log4j
    private Logger logger = Logger.getLogger(BasePage.class);



    public WebDriver getDriver(){
        return BaseCase.driver;
    }

    /**
     * 等待元素可见
     */
    public WebElement waitForElementVisible(By by){
        logger.info("等待元素【"+by+"】可见");
        WebDriverWait webDriverWait = new WebDriverWait(getDriver(), 5);
        webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(by));
        return getDriver().findElement(by);
    }

    /**
     * 清楚元素中的值
     * @param by
     */
    public void clear(By by){
        logger.info("清除了元素【"+by+"】的值");
        waitForElementVisible(by).clear();
    }


    /**
     * 通过执行js代码，将div关闭
     */
//    public void dismissDiv(By divBy){
//        JavascriptExecutor javascriptExecutor = (JavascriptExecutor) getDriver();
//        WebElement findElement = waitForElementVisible(divBy);
//        javascriptExecutor.executeScript()
//    }

    /**
     * 输入
     */
    public void type(By by, String data){
        logger.info("在元素【"+by+"】中输入数据【"+data+"】");
        waitForElementVisible(by).sendKeys(data);
    }

    /**
     * 等待元素可点击
     */
    public WebElement waitForElementTobeClickable(By by){
        logger.info("等待元素【"+by+"】可点击");
        WebDriverWait webDriverWait = new WebDriverWait(getDriver(), 5);
        webDriverWait.until(ExpectedConditions.elementToBeClickable(by));
        return getDriver().findElement(by);
    }

    /**
     * 点击
     */
    public void click(By by){
        logger.info("点击元素【"+by+"】");
        waitForElementTobeClickable(by).click();
    }



    /**
     * 获取元素文本
     */
    public String getText(By by){
        logger.info("获取元素【"+by+"】的文本值");
        WebElement webElement = null;
        try {
            webElement = waitForElementVisible(by);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return webElement.getText();
    }

    /**
     * 等待元素存在，存在返回true，不存在返回false
     */
    public boolean waitForElementPresent(By by){
        logger.info("查找元素【"+by+"】是否存在");
        try {
            waitForElementVisible(by);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 切换iframe
     */
    public void switchIframe(By by){
        logger.info("切换iframe【"+by+"】");
        WebDriverWait webDriverWait = new WebDriverWait(getDriver(), 5);
        //iframe可用并且切换到里面去
        webDriverWait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by));
    }
}
